/*
 * TableModelZonas.java
 *
 * Created on 4 de febrero de 2007, 11:59 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package ig.util;

import ig.metrica.riesgo.AjustesAlternosDTO;
import ig.metrica.riesgo.ZonaDTO;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Hashtable;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author jsanchez
 */
public class TableModelAlternos extends AbstractTableModel {
//    private Collection<AjustesAlternosDTO> collection=null;
    private boolean[]   editables={true,false};
    private String[] columnNames = {"Ajuste Horizontal entre Zonas", "Zona"};
    private Hashtable<Integer,ZonaDTO> hashTable=new Hashtable<Integer,ZonaDTO>();
    private ZonaDTO tmp=null;
    private Integer i=0;
    private Collection<ZonaDTO> c1=new ArrayList<ZonaDTO>();
    private Collection<AjustesAlternosDTO> c2=new ArrayList<AjustesAlternosDTO>();
    /** Creates a new instance of TableModelZonas */
    public TableModelAlternos(Collection<AjustesAlternosDTO> collection,Collection<ZonaDTO> c1) {
        this.c1=c1;
        this.c2=collection;
    }
    ZonaDTO  zonaActual=null;
    Object[][] data=null;
    public synchronized void setZonas(Collection<ZonaDTO> c1,ZonaDTO zonaActual){
        hashTable=new Hashtable<Integer,ZonaDTO>();
        this.c1=c1;
        this.zonaActual=zonaActual;
        i=0;
        data = new Object[c1.size()<=0?0:c1.size()-1][2];
        for (ZonaDTO elem : c1) {
            if(!elem .getId().equals(zonaActual.getId())){
                hashTable.put(i,elem);
                data[i][1]=elem;
                data[i][0]=getAjusteAlterno(zonaActual.getId(),elem.getId());
                i++;
            }
        }
        fireTableDataChanged();
    }
    private Double getAjusteAlterno(Integer id1,Integer id2){
        if(c2==null)
            c2=new ArrayList<AjustesAlternosDTO>();
//        System.out.println("c2:"+c2.size());
        for (AjustesAlternosDTO elem : c2) {
            if((elem.getIdZona1().equals(id1) && elem.getIdZona2().equals(id2))|| 
                    (elem.getIdZona1().equals(id2) && elem.getIdZona2().equals(id1))){
                return elem.getAjuste();
            }
        }
        AjustesAlternosDTO ajustes=new AjustesAlternosDTO(id1,id2,0.0);
        ajustes.setNuevo(true);
        c2.add(ajustes);
        return 0.0;
    }
    public void setAjustes(Collection<AjustesAlternosDTO> _c1){
        this.c2=_c1;
    }
    public String getColumnName(int col) {
        return columnNames[col];
    }
    public int getRowCount() {
        return hashTable.size();
    }
    
    public int getColumnCount() {
        return columnNames.length;
    }
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }
    
    public boolean isCellEditable(int row, int col) {
        return (editables[col]==true) ? true : false;
        
    }
    
    public Object getValueAt(int row, int col) {
//        System.out.println("data.leg:"+data.length);
        return data[row][col];
    }
    
    
    public void setValueAt(Object value, int row, int col){
        this.tmp=this.hashTable.get(row);                
        if(col==0){
            System.out.println("actual:"+zonaActual.getId());
            System.out.println("tmp:"+tmp.getId());
            for (AjustesAlternosDTO elem : c2) {                
                if((elem.getIdZona1().equals(zonaActual.getId()) && elem.getIdZona2().equals(tmp.getId()))|| 
                        (elem.getIdZona1().equals(tmp.getId()) && elem.getIdZona2().equals(zonaActual.getId()))){
                    elem.setAjuste(Double.valueOf(value.toString()));
                    data[row][col]=value;
                }
                System.out.println(elem.getIdZona1()+"->"+elem.getIdZona2()+"->"+elem.getAjuste());                
            }
        }
        this.fireTableCellUpdated(row,col);
    }
    
    public Collection<AjustesAlternosDTO> getData(){
        for (AjustesAlternosDTO elem : c2) {
            System.out.println(elem.getIdZona1()+"->"+elem.getIdZona2()+"->"+elem.getAjuste());
        }
        return this.c2;
    }
    
    public synchronized void removeZona(Integer integer) {
        if(c2!=null)
            try {
                for (AjustesAlternosDTO elem : c2) {
                    if(elem.getIdZona1().equals(integer) || elem.getIdZona2().equals(integer)){
                        c2.remove(elem);
                    }
                }
            } catch (java.util.ConcurrentModificationException e) {
                System.out.println(integer);
                try {
                    Thread.sleep(1500);
                } catch (Exception ex) {}
                this.removeZona(integer);
            }
            fireTableDataChanged();
    }
}
